$(function(){

	// 隐藏导航跟底部
	var windowheight = $(window).height();
	var margintop = (windowheight-$(".loginPublic").height())/2-55;
	$(".site-footer").css("display","none");
	$(".header").css("display","none");
	$(".loginPublic").css("margin-top",margintop);
	$(document).on( "click", "#subRegister", function(e) {
		var register = $("#registration");
		var rusername = $(this).parent().parent().find("#rusername");
		var rpassword =$(this).parent().parent().find("#rpassword");
		var rphonecode = $(this).parent().parent().find("#rphonecode");
		var password = $(this).parent().parent().find("#rpassword").val();
		var username = $(this).parent().parent().find("#rusername").val();
		var post_id =  JSON.parse(localStorage.getItem('post_id'));
		var loading;
			if(checkusername(rusername) && code(rphonecode) && checkPWd(rpassword,1)){
				loading = layer.msg('加载中...', {icon: 16});
				$.post(register[0].action,register.serialize(),
				function(data){
				}).error(function(data) {
				    var obj = $.parseJSON(data.responseText);
					layer.close(loading);   
					$.each(obj.error_messages, function(i, item){
						$(".default-tip").hide();
						$(".error-tip").show();
						$(".error-tip").text(item[0]);
					});
					$(this).parent().parent().find(".imgObj").attr("src",'captcha?' + Math.random());
				}).success(function(data) {
					layer.close(loading);
					// 注册成功自动登录
					$.post(jumpUrl+"/login", {"password":password,"username":username,"post_id":post_id},function(data){
				        var token = data.token;
				        localStorage.setItem("token", JSON.stringify(token));
				        if(post_id != null){
				          var post_id_status = true;
				          localStorage.setItem("post_id_status", JSON.stringify(post_id_status));
				        }
				      var goback = sessionStorage.getItem("goback");
				      if(goback == null || goback=="" || goback=="undefined"){
				        location.href=jumpUrl;
				      }else{
				          location.href= goback;
				      }
				      sessionStorage.removeItem("goback");
				      }).error(function(data) {
				    });
				});
				return true;
			}else{
				return false;
			}
		stopDefault(e);
	})
	

	//第一次获取验证码后要60秒以后才能在次获取
	// var t, timePromise;
	// t = 60;  
	// var runTiming = function(){
	//   timePromise = setInterval(function(){
	// 		t -= 1;
	// 		console.info(t);
	// 		//console.info($('.messges').text())
	// 		//$('.phonecode').text("重新发送("+t+")");
	// 		$('.messges').text("重新发送("+t+")");
	// 		if(t == 0){
	// 			$('.messges').text("重新发送");
	// 			clearInterval(timePromise)
	//   			timePromise = undefined;
	// 		}
	//   }, 1000);
	//   return timePromise;
	// }


	$(document).on( "click", "#phonecode", function(e) {
		// t = 60;
		// clearInterval(timePromise)
	 //  	timePromise = undefined;
	
		var usernameval = $(this).parent().parent().find("#rusername").val();
		var username = $(this).parent().parent().find("#rusername");
		var phonecode_href = $("#phonecode").attr("href");
		if(checkusername(username)){
			loading = layer.msg('加载中...', {icon: 16});
			$.post(phonecode_href, {"phone":usernameval, "_token":$("[name='_token']").val()}, function(data){
			}, 'json').error(function(data) {
				layer.close(loading);
			    var obj = $.parseJSON(data.responseText);
				$.each(obj.error_messages, function(i, item){
					$(".default-tip").hide();
					$(".error-tip").show();
					$(".error-tip").text(item[0]);
				});   
			}).success(function(data) {
				layer.close(loading);

			 //   	var token = data.token;
			 //   	localStorage.setItem("token", JSON.stringify(token));
			 //   	var locationhref = localStorage.getItem("locationhref");
				// window.location.href=locationhref;
				// alert(data.success_messages);
				/*点击获取验证码进入倒计时*/
				var time=60; 
				var id = setInterval(function() {
			    	if (time >0) {
			    		time--; 
			    		$('.phonecode').text("已发送("+time+")");
			    		$(".phonecode").css("background-color","#b5b5b5");
			    		$(".phonecode").attr("id","");
			    	} else {
		  			    $(".phonecode").text("获取验证码");
		  			    $(".phonecode").css("background-color","#48b4ff");
		  			    $(".phonecode").attr("id","phonecode");
		  			 clearInterval(id);
		  			};
				},1000);
			});
		}else{
			return false;
		}
	})

	$(document).on('submit', '.deform', function(e) {
		stopDefault(e);
	})
	$('#rphonecode').bind('input propertychange', function(e) {
			$('#rphonecode').unbind('input propertychange');
			stopDefault(e);
	});
	$('#rusername').bind('input propertychange', function(e) {
			$('#rusername').unbind('input propertychange');
			stopDefault(e);
	});
	$('#rpassword').bind('input propertychange', function(e) {
			$('#rpassword').unbind('input propertychange');
			stopDefault(e);
	});
	
})


